home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 194_01 / msort.c < prev    next >
Text File  |  1985-11-13  |  4KB  |  205 lines

  1. /* [MSORT.C of JUGPDS Vol.17]
  2. *****************************************************************
  3. *                                *
  4. *    Written by  Hakuo Katayose (JUG-CP/M No.179)        *
  5. *            49-114 Kawauchi-Sanjuunin-machi        *
  6. *            Sendai, Miyagi 980                          *
  7. *            Phone: 0222-61-3219                *
  8. *                                *
  9. *    Edited & tested by Y. Monma (JUG-C/M Disk Editor)       * 
  10. *                                *
  11. *****************************************************************
  12. */
  13.  
  14. /* msort - sort of text (ASCII) file 
  15.  
  16.     Usage: msort -{dfnlr} <infile >outfile ^Z");
  17. where
  18.     -d: dictionary order 
  19.     -f: fold order
  20.     -n: numeric order
  21.     -l: line # listing
  22.     -r: reverse (decreasing) order 
  23.  
  24. */
  25.  
  26. #include "stdio.h"
  27. #include "def.h"
  28. #include <dio.h>
  29.  
  30. #define LINES    2000
  31. #define MAXLEN    128
  32. #define MAXBUF    30
  33.  
  34. char    allocbuf[1024][MAXBUF], *alloc_bp, *alloc_ep;
  35.  
  36. char    opt_d;        /* dictionary order */
  37. char    opt_f;        /* fold order        */
  38. char    opt_n;        /* numeric order    */
  39. char    opt_l;        /* line_no listing  */
  40. char    opt_r;        /* reverse order    */
  41.  
  42.  
  43. main(argc, argv)
  44. int argc;
  45. char *argv[];
  46.  
  47. {
  48.     char    *ap;
  49.     int    i;
  50.  
  51.     dioinit(&argc, argv);
  52.     if (argc < 3) {
  53.         error("Usage: msort -{dfnlr} <infile >outfile ^Z");
  54.         exit();
  55.         }
  56.     else
  57.     opt_d = opt_f = opt_n = opt_l = opt_r = OFF;
  58.     i = 0;
  59.     while (--argc > 0 && (*++argv)[0] == '-')
  60.         for (ap = argv[0]+1; *ap != '\0'; ap++)
  61.             switch (tolower(*ap)) {
  62.             case 'd':
  63.                 opt_d = ON;
  64.                 break;
  65.             case 'f':
  66.                 opt_f = ON;
  67.                 break;
  68.             case 'n':
  69.                 opt_n = ON;
  70.                 break;
  71.             case 'l' :
  72.                 opt_l = ON;
  73.                 break;
  74.             case 'r':
  75.                 opt_r = ON;
  76.                 break;
  77.             }
  78.     fileSort();
  79.     dioflush();
  80. }
  81.  
  82.  
  83. FileSort()
  84. {
  85.     char    *lineptr[LINES];
  86.     int    nlines, high;
  87.     FILE    fp1, fp2;
  88.  
  89.     high = 0;
  90.     if (Gtext(lineptr, &nlines, LINES, STDIN) == EOF) {
  91.         Sort(lineptr, nlines);
  92.         Ptext(lineptr, nlines, STDOUT);
  93.         }
  94.     else
  95.         fprintf(STDERR, "Input too big to sort\n" );
  96. }
  97.  
  98.  
  99. Sort(lineptr, nlines)
  100. char *lineptr[];
  101. {
  102.     int    strdfcmp(), strfcmp(), strcmp(), strdcmp(), numcmp(), swap();
  103.  
  104.     if (opt_n == ON)
  105.         quick(lineptr, 0, nlines-1, numcmp, swap);
  106.     else if (opt_d == ON && opt_f == ON)
  107.         quick(lineptr, 0, nlines-1, strdfcmp, swap);
  108.     else if (opt_d == ON)
  109.         quick(lineptr, 0, nlines-1, strdcmp, swap);
  110.     else if (opt_f == ON)
  111.         quick(lineptr, 0, nlines-1, strfcmp, swap);
  112.     else
  113.         quick(lineptr, 0, nlines-1, strcmp, swap);
  114. }
  115.  
  116.  
  117. Gtext(lineptr, nlines, maxlines, fp)
  118. char    *lineptr[];
  119. int    *nlines;
  120. FILE    *fp;
  121. {
  122.     int    len;
  123.  
  124.     *nlines = 0;
  125.     alloc_bp = allocbuf;
  126.     alloc_ep = &allocbuf[1023][(MAXBUF-1)];
  127.     do {
  128.         if ((len = fgetlin(fp, alloc_bp, MAXLEN)) <= 0)
  129.             return EOF;
  130.         lineptr[(*nlines)++] = alloc_bp;
  131.         alloc_bp += len;
  132.         *(alloc_bp - 1) = '\0';
  133.         } while (alloc_bp + MAXLEN <= alloc_ep && *nlines < maxlines);
  134.     return(len);
  135. }
  136.  
  137.  
  138. Ptext(lineptr, nlines, fp)
  139. char    *lineptr[];
  140. int    nlines;
  141. FILE    *fp;
  142. {
  143.     int    lno;
  144.  
  145.     lno = 1;
  146.     if( opt_r == ON ) lineptr += nlines;
  147.     while (nlines--) {
  148.         if( opt_l == ON )
  149.             fprintf(fp, "%6u: ", lno++);
  150.         fprintf(fp, "%s\n", ( opt_r == ON ? *--lineptr : *lineptr++) );
  151.         }
  152. }
  153.  
  154.  
  155. numcmp(s1, s2)
  156. char *s1, *s2;
  157. {
  158.     int    atoi(), v1, v2;
  159.  
  160.     v1 = atoi(s1);
  161.     v2 = atoi(s2);
  162.     if(v1 < v2)
  163.         return(-1);
  164.     else if(v1 > v2)
  165.         return(1);
  166.     else
  167.         return(0);
  168. }
  169.  
  170.  
  171. quick(v, l, r, comp, exch)
  172. char    *v[];
  173. int    l, r;
  174. int    (*comp)(), (*exch)();
  175. {
  176.     int    vx, i, j;
  177.  
  178.     i = l;  j = r;
  179.     vx = v[ (l+r)/2 ];
  180.     while(i <= j) {
  181.         while( (*comp)(v[i], vx) < 0 )
  182.             i++;
  183.         while( (*comp)(vx, v[j]) < 0 )
  184.             j--;
  185.         if(i <= j) {
  186.             (*exch)(&v[j], &v[i]);
  187.             i++;
  188.             j--;
  189.             }
  190.         }
  191.     if(l < j) quick(v,l,j,comp,exch);
  192.     if(i < r) quick(v,i,r,comp,exch);
  193. }
  194.  
  195.  
  196. swap(px, py)
  197. char    *px[], *py[];
  198. {
  199.     char    *temp;
  200.  
  201.     temp = *px;
  202.     *px = *py;
  203.     *py = temp;
  204. }
  205.